<template>
  <div class="login-container">
    <el-form

        :model="loginForm"

        label-width="80px"
        class="login-form"
    >
      <el-form-item label="账号" prop="username">
        <el-input v-model="loginForm.username" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import axios from "axios";
import {ElMessage} from "element-plus";
import router from "@/router";

// 表单引用
// const loginFormRef = ref(null);

// 表单数据
const loginForm = ref({
  username: '',
  password: '',
});

// // 表单验证规则
// const loginRules = ref({
//   username: [
//     { required: true, message: '请输入账号', trigger: 'blur' },
//   ],
//   password: [
//     { required: true, message: '请输入密码', trigger: 'blur' },
//   ],
// });

// 登录处理方法
const handleLogin = () => {
  console.log(loginForm.value);
axios.post(BASE_URL+'/v1/user/login',loginForm.value).then((response)=>{
  console.log(response.data.data);
  if (response.data.code==2000){
    ElMessage.success("登录成功！");
    localStorage.setItem("user",JSON.stringify(response.data.data));//丁：这里的user是从数据库拿到的所有，如果谁有需要可以自行添加
    router.push('workbench');
  }else{
    ElMessage.error("登录失败！");
    loginForm.value={username: '', password: ''};
  }
});
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.login-form {
  width: 300px;
}
</style>